<!-- 已过时 -->
<template>
  <Dialog :title="t('studentPatent.audit.title')" v-model="dialogVisible" width="1000">
    <div v-loading="formLoading">
      <el-result
        v-if="!initializing && !patentDataPrevious"
        icon="info"
        :title="t('studentPatent.audit.firstTip')"
        :sub-title="t('studentPatent.audit.firstTip2')"
      />
      <div class="mb-[25px]" v-if="patentData && patentDataPrevious">
        <StudentPatentDiffView
          :patent-info="patentData"
          :patent-info-archive="patentDataPrevious"
        />
      </div>
      <el-descriptions :column="1" border label-width="150">
        <el-descriptions-item :label="t('studentPatent.fields.reviewStatus')" width="380">
          <el-radio-group v-model="auditData.reviewStatus">
            <el-radio value="已通过">{{ t('studentPatent.audit.pass') }}</el-radio>
            <el-radio value="已拒绝" class="deny">{{ t('studentPatent.audit.deny') }}</el-radio>
          </el-radio-group>
        </el-descriptions-item>
        <el-descriptions-item :label="t('studentPatent.fields.reviewComments')" width="380">
          <el-input
            class="simple-input"
            v-model="auditData.reviewComments"
            type="textarea"
            :rows="3"
            :maxlength="300"
            show-word-limit
            :placeholder="
              t('common.inputTextPlaceholder') + t('studentPatent.fields.reviewComments')
            "
          />
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {
  StudentPatentApi,
  StudentPatentAuditReqVO,
  StudentPatentVO
} from '@/api/core/studentpatent'
import StudentPatentDiffView from './StudentPatentDiffView.vue'

/** 专利 审核 */
defineOptions({ name: 'StudentPatentAudit' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const initializing = ref(false) // 表单首次数据获取中
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用

const auditData = ref<StudentPatentAuditReqVO>({
  id: '',
  reviewStatus: '',
  reviewComments: ''
})

const patentData = ref<StudentPatentVO>()
const patentDataPrevious = ref<StudentPatentVO>()

/** 打开弹窗 */
const open = async (id: string) => {
  auditData.value = {
    id: '',
    reviewStatus: '',
    reviewComments: ''
  }
  dialogVisible.value = true
  initializing.value = true
  formLoading.value = true
  auditData.value.id = id
  try {
    const response = await StudentPatentApi.getStudentPatentAudit(id)
    patentData.value = response.current
    if (patentData.value && !patentData.value?.attachment) {
      patentData.value!.attachment = '[]'
    }
    patentDataPrevious.value = response.previous
    if (patentDataPrevious.value && !patentDataPrevious.value.attachment) {
      patentDataPrevious.value.attachment = '[]'
    }
  } finally {
    initializing.value = false
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 基本校验
  if (auditData.value.reviewStatus === '') {
    message.error(t('common.selectTextPlaceholder') + t('studentPatent.fields.reviewStatus'))
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    await StudentPatentApi.auditStudentPatent(auditData.value)
    message.success(t('common.auditSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
.simple-input {
  :deep(.el-textarea__inner) {
    border-radius: 0 !important;
    box-shadow: none !important;
    padding: 0 !important;
    border-bottom: 1px solid var(--el-border-color);
  }
}

.deny {
  :deep(.is-checked .el-radio__inner) {
    background: var(--el-color-danger);
    border-color: var(--el-color-danger);
  }
  :deep(.el-radio__label) {
    color: var(--el-color-danger);
  }
}
</style>
